package ar.com.fiuba.application.impl;

import java.util.HashMap;
import java.util.Map;

import ar.com.fiuba.SystemKeys;
import ar.com.fiuba.application.api.Application;
import ar.com.fiuba.service.api.Service;

public class ApplicationImpl implements Application {
	private Map<String, Service> applicationModes;

	public ApplicationImpl(Map<String, Service> applicationModes) {
		this.applicationModes = applicationModes;
	}

	public void run(String[] args) {
		String mode = args[0];
		Service service = applicationModes.get(mode.replaceAll("-", ""));
		service.run(extractParameters(mode, args));
	}

	private Map<String, String> extractParameters(String mode, String[] args) {
		Map<String, String> ret = new HashMap<String, String>();
		if (SystemKeys.LOAD_MODE.equals(mode)) {
			ret.put(SystemKeys.DATA_BASE_LOADER_FILE_PATH, args[1]);
		}
		if (SystemKeys.CORRECT_MODE.equals(mode)) {
			ret.put(SystemKeys.COEFFICIENT_ALGORYTHM, args[1]);
			ret.put(SystemKeys.INPUT_WORD, args[2]);
		}
		if (SystemKeys.REPORT_MODE.equals(mode)) {
			ret.put(SystemKeys.COEFFICIENT_ALGORYTHM, args[1]);
			ret.put(SystemKeys.REPORT_OUTPUT_FOLDER, args[2]);
		}
		return ret;
	}
}
